package com.eshop.modules.system.rest;

import org.springframework.web.bind.annotation.DeleteMapping;
import java.io.Serializable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import com.eshop.modules.aop.ForbidSubmit;
import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.exception.BadRequestException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.modules.system.domain.Dict;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import io.swagger.annotations.ApiOperation;
import com.eshop.logging.aop.log.Log;
import java.io.IOException;
import java.util.List;
import com.eshop.modules.system.service.dto.DictDto;
import com.eshop.modules.system.service.dto.DictQueryCriteria;
import javax.servlet.http.HttpServletResponse;
import com.eshop.dozer.service.IGenerator;
import com.eshop.modules.system.service.DictService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;

@Api(tags = { "系统：字典管理" })
@RestController
@RequestMapping({ "/api/dict" })
public class DictController
{
    private final DictService dictService;
    private final IGenerator generator;
    private static final String ENTITY_NAME = "dict";
    
    public DictController(final DictService dictService, final IGenerator generator) {
        this.dictService = dictService;
        this.generator = generator;
    }
    
    @Log("导出字典数据")
    @ApiOperation("导出字典数据")
    @GetMapping({ "/download" })
    @PreAuthorize("@el.check('admin','dict:list')")
    public void download(final HttpServletResponse response, final DictQueryCriteria criteria) throws IOException {
        this.dictService.download(this.generator.convert((List)this.dictService.queryAll(criteria), (Class)DictDto.class), response);
    }
    
    @Log("查询字典")
    @ApiOperation("查询字典")
    @GetMapping({ "/all" })
    @PreAuthorize("@el.check('admin','dict:list')")
    public ResponseEntity<Object> all() {
        return new ResponseEntity(this.dictService.queryAll(new DictQueryCriteria()), HttpStatus.OK);
    }
    
    @Log("查询字典")
    @ApiOperation("查询字典")
    @GetMapping
    @PreAuthorize("@el.check('admin','dict:list')")
    public ResponseEntity<Object> getDicts(final DictQueryCriteria resources, final Pageable pageable) {
        return new ResponseEntity(this.dictService.queryAll(resources, pageable), HttpStatus.OK);
    }
    
    @Log("新增字典")
    @ApiOperation("新增字典")
    @PostMapping
    @PreAuthorize("@el.check('admin','dict:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody final Dict resources) {
        if (resources.getId() != null) {
            throw new BadRequestException("A new dict cannot already have an ID");
        }
        return new ResponseEntity(this.dictService.save(resources), HttpStatus.CREATED);
    }
    
    @ForbidSubmit
    @Log("修改字典")
    @ApiOperation("修改字典")
    @PutMapping
    @PreAuthorize("@el.check('admin','dict:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody final Dict resources) {
        this.dictService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }
    
    @ForbidSubmit
    @Log("删除字典")
    @ApiOperation("删除字典")
    @DeleteMapping({ "/{id}" })
    @PreAuthorize("@el.check('admin','dict:del')")
    public ResponseEntity<Object> delete(@PathVariable final Long id) {
        this.dictService.removeById((Serializable)id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
